package com.wing.wcabinet.controller.app;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.wcabinet.model.entity.WcabCellInventoryInout;
import com.wing.wcabinet.model.response.WcabCellInventoryInoutDetailVO;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.wcabinet.model.entity.WcabCellInventoryUser;
import com.wing.wcabinet.model.response.WcabCellInventoryUserVO;
import com.wing.wcabinet.service.WcabCellInventoryUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.wcabinet.model.request.WcabCellInventoryUserForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-用户产品在库库存管理", tags = "用户产品在库库存管理")
@RestController("appWcabCellInventoryUserController")
@RequestMapping("cabinet/app/wcabCellInventoryUser")
public class WcabCellInventoryUserController {

    @Autowired
    private WcabCellInventoryUserService wcabCellInventoryUserService;
    @Autowired
    private LocaleMessage localeMessage;

    @GetMapping()
    public PageResult<WcabCellInventoryUserVO> list(
            @RequestParam(name = "page",defaultValue = "1") Integer page
            ,@RequestParam(name = "limit",defaultValue = "10") Integer limit
            ,@RequestParam(name = "sort",defaultValue = "id") String sort
            ,@RequestParam(name = "order",defaultValue = "asc") String order
    ) {
        Page<WcabCellInventoryUserVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }

        Page<WcabCellInventoryUserVO> list = wcabCellInventoryUserService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }



    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long wcabCellInventoryUserId) {
        WcabCellInventoryUserVO wcabCellInventoryUserVO = wcabCellInventoryUserService.get4appById(wcabCellInventoryUserId);
        if (wcabCellInventoryUserVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), wcabCellInventoryUserVO);
    }

    @PostMapping()
    public JsonResult add(@Validated WcabCellInventoryUserForm wcabCellInventoryUserForm) {
        if (wcabCellInventoryUserService.add(wcabCellInventoryUserForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
